package com.lj.demo.msmservice.controller;

import com.lj.demo.commonutils.R;
import com.lj.demo.msmservice.service.MsmService;
import com.lj.demo.msmservice.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashMapperProvider;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: liujiang
 * @Date: 2020/12/22/23:02
 * @Description:
 */
@RestController
@RequestMapping("/msmService/msm")
@CrossOrigin
public class MsmApiController {
    @Autowired
    private MsmService msmService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @GetMapping(value = "/send/{phone}")
    public R code(@PathVariable String phone){
        String code = redisTemplate.opsForValue().get(phone);
        if (!StringUtils.isEmpty(code)) return R.ok();

//        code = RandomUtil.getSixBitRandom();
//        Map<String,Object> param = new HashMap<>();
//        param.put("code",code);
//        boolean isSend = msmService.send(phone,param);
//        if (isSend){
        //模拟加入一个验证码
            redisTemplate.opsForValue().set(phone,"8888",2, TimeUnit.MINUTES);
            return R.ok();
      //  }
       // return R.error().message("发送短信失败");
    }


}
